/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.h2t.loveplace.location;

import com.nokia.mid.location.LocationUtil;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

/**
 *
 * @author PHAN THAI
 */
public class LocationProviderFactory {
 
    public static final int CELL_ID = 0;
    public static final int GPS = 1;
    private static LocationProvider provider;
    private static String title;
    private static final LocationProviderFactory INSTANCE =
            new LocationProviderFactory();
 
    /**
     *  I'm a singleton.
     */
    private LocationProviderFactory() {
    }
 
    /**
     * Start locating at a default interval, timeout and maxAge;
     * @param listener - an listener who will receive location updates.
     */
    public static void startLocating(LocationListener listener) {
        if (provider != null) {
            provider.setLocationListener(listener, -1, -1, -1);
        }
    }
 
    /**
     * Stop sending location updates to the currently registered listener
     */
    public static void stopLocating() {
        if (provider != null) {
            provider.setLocationListener(null, -1, -1, -1);
        }
    }
 
    /**
     * 
     * @return The name of the current provider.
     */
    public static String getTitle() {
        return title;
    }
 
    /**
     * Obtains a provider to be
     * @param type
     * @return  The current location provider, or NULL if unable to set it up.
     * @throws LocationException 
     */
    public static LocationProvider setProvider(int type) {
 
        try {
            if (type == CELL_ID) {
                provider = INSTANCE.getCellIdProviderInstance();
                title = "Obtaining Cell Id...";
            } else if (type == GPS) {
                provider = INSTANCE.getGPSProviderInstance();
                title = "Obtaining GPS...";
            }
 
        } catch (LocationException lex) {
             provider = null;
            title = "Provider not found...";
        } catch (NoClassDefFoundError ncdfex) {
            provider = null;
            title = "Provider not supported...";
        }
 
        return provider;
    }
 
    /**
     * 
     * @return A Cell Id Location Provider
     * @throws LocationException  if the provider could not be found.
     */
    private LocationProvider getCellIdProviderInstance() throws LocationException {
        int[] methods = {Location.MTA_ASSISTED | Location.MTE_CELLID | Location.MTY_NETWORKBASED};
        return LocationUtil.getLocationProvider(methods, null);
    }
 
    /**
     * 
     * @return A GPS Location Provider
     * @throws LocationException  if the provider could not be found.
     */
    private LocationProvider getGPSProviderInstance() throws LocationException {
        Criteria criteria = new Criteria();
 
        criteria.setCostAllowed(true);
        criteria.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
        criteria.setSpeedAndCourseRequired(false);
        criteria.setAltitudeRequired(false);
        criteria.setAddressInfoRequired(false);
        return LocationProvider.getInstance(criteria);
    }
}
